package com.dungeon.game;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dungeon.game.Constants.Character_action_code;
import com.dungeon.game.Constants.Class_code;
import com.dungeon.game.Constants.Game_mode;
import com.dungeon.game.Constants.Position_code;
import com.dungeon.game.Constants.Stance_code;
import com.dungeon.game.Constants.Status_effect_code;
import com.dungeon.game.Party.Character;

public class PartyPane extends View {

	public PartyPane(Context context) {
		super(context);
		init();
		init_detector(context);
	}

	public PartyPane(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		init_detector(context);
	}

	public PartyPane(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		init_detector(context);
	}

	public MyGestureListener listener;
	private GestureDetector detector;

	public void init_detector(Context context){
		listener = new MyGestureListener(context);
		detector = new GestureDetector(context,listener);
	}

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    	Context context;
    	MainActivity activity;

    	public MyGestureListener(Context _context){
    		super();
    		context = _context;
    	}

        public boolean onDown(MotionEvent event) {
        	for (Position_code pos : Arrays.asList(Position_code.POS_C_BACK_1,Position_code.POS_C_BACK_2,Position_code.POS_C_BACK_3,Position_code.POS_C_FRONT_1,Position_code.POS_C_FRONT_2,Position_code.POS_C_FRONT_3)){
        		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
        			Character c = Gamestate.p.get_char_at_pos(pos);
        			if (c!=null && c.can_act()){
        				Gamestate.dragging = c;
        				Gamestate.started_dragging = Gamestate.w.clock;
        			}
        		}
        	}
        	return true;
        }

        public boolean onDoubleTap(MotionEvent event) {
        	// havent been getting far with this
        	return false;
        }

        public boolean onSingleTapUp(MotionEvent event) {
        	Gamestate.dragging = null;
        	Game_mode this_mode = Gamestate.mode;
        	double started_dragging = Gamestate.started_dragging;
        	double clock = Gamestate.w.clock;
        	if (Gamestate.mode==Game_mode.MODE_NORMAL){
        		Gamestate.actor = null;
            	for (Position_code pos : Arrays.asList(Position_code.POS_C_BACK_1,Position_code.POS_C_BACK_2,Position_code.POS_C_BACK_3,Position_code.POS_C_FRONT_1,Position_code.POS_C_FRONT_2,Position_code.POS_C_FRONT_3)){
            		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
            			Character c = Gamestate.p.get_char_at_pos(pos);
            			if (c!=null && c.alive && !c.about_to_die){
            				Gamestate.actor = c;
            			}
            		}
            	}
            	if (Gamestate.actor!=null && Gamestate.actor.current_stance==Stance_code.ST_RETRENCH)
            		Gamestate.actor.current_stance = null;
            	if (Gamestate.actor!=null && Gamestate.actor.has_status_effect(Status_effect_code.EF_SLEEP)){
            		Gamestate.actor.status_effects.get(Status_effect_code.EF_SLEEP).level = Gamestate.actor.status_effects.get(Status_effect_code.EF_SLEEP).level-1;
                    if (Gamestate.actor.status_effects.get(Status_effect_code.EF_SLEEP).level<=0)
                    	Gamestate.actor.status_effects.get(Status_effect_code.EF_SLEEP).lasts_until = 0;
            	} else if (Gamestate.actor!=null && Gamestate.actor.can_act()){
    	    		for (Character_action a : Gamestate.actor.actions){
    	    			if (a.callable()){
    	        			Gamestate.mode = Game_mode.MODE_ACTION;
    	        			Gamestate.actionchooser_created = false;
    	        			break;
    	    			}
    	    		}
    			} else {
        			Gamestate.mode = Game_mode.MODE_NORMAL;
        		}
        	} else if (Gamestate.mode == Game_mode.MODE_FRIENDLY_TARGET){
        		Character potential_target = null;
            	for (Position_code pos : Arrays.asList(Position_code.POS_C_BACK_1,Position_code.POS_C_BACK_2,Position_code.POS_C_BACK_3,Position_code.POS_C_FRONT_1,Position_code.POS_C_FRONT_2,Position_code.POS_C_FRONT_3)){
            		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
            			Character c = Gamestate.p.get_char_at_pos(pos);
            			if (c!=null && c.alive && !c.about_to_die){
            				potential_target = c;
            			}
            		}
            	}
            	if (potential_target!=null){
            		if (Gamestate.action.friend_target_ok(potential_target)){
            			Gamestate.action.char_target = potential_target;
            			Gamestate.action.trigger();
            		}
            	}
        		Gamestate.mode=Game_mode.MODE_NORMAL;
        	} else {
        		Gamestate.mode=Game_mode.MODE_NORMAL;
          	}
        	return false;
        }

        public void onLongPress(MotionEvent event) {
        	Character td = null;
        	for (Position_code pos : Arrays.asList(Position_code.POS_C_BACK_1,Position_code.POS_C_BACK_2,Position_code.POS_C_BACK_3,Position_code.POS_C_FRONT_1,Position_code.POS_C_FRONT_2,Position_code.POS_C_FRONT_3)){
        		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
        			Character c = Gamestate.p.get_char_at_pos(pos);
        			if (c!=null && c.alive && !c.about_to_die){
        				td = c;
        			}
        		}
        	}
        	if (td!=null){
    	        AlertDialog.Builder builder = new AlertDialog.Builder(context);
    	        ScrollView sv = new ScrollView(context);
    	        sv.setBackgroundColor(Color.argb(0,0,0,0));
    	        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    	        LinearLayout ll = new LinearLayout(context);
    	        ll.setBackgroundColor(Color.argb(0,0,0,0));
    	        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    	        ll.setOrientation(LinearLayout.VERTICAL);
    	        ll.setGravity(Gravity.TOP);
    	        ll.addView(twotext(context,"Class:",Constants.class_code_to_name.get(td.class_code)));
    	        ll.addView(twotext(context,"Level:",td.level));
    	        ll.addView(heading(context,"Status"));
    	        ll.addView(twotext(context,"Health:",(td.health) + " / " + (td.max_health)));
    	        ll.addView(twotext(context,"Bar:",(td.energy) + " / " + (td.max_energy)));
    	        if (td.current_stance==Stance_code.ST_BERSERK)
    	        	ll.addView(onetext_italic(context,"Berserk"));
    	        else if (td.current_stance==Stance_code.ST_INVULNERABLE || td.has_status_effect(Status_effect_code.EF_INVULNERABLE))
    	        	ll.addView(onetext_italic(context,"Invulnerable"));
    	        else if (td.current_stance==Stance_code.ST_EPIC_BLOCK)
    	            ll.addView(onetext_italic(context,"Epic Blocking"));
    	        else if (td.current_stance==Stance_code.ST_LEAP_ASIDE)
    	            ll.addView(onetext_italic(context,"Leaping Aside"));
    	        else if (td.current_stance==Stance_code.ST_BLOCK)
    	            ll.addView(onetext_italic(context,"Blocking"));
    	        else if (td.current_stance==Stance_code.ST_CATCH)
    	            ll.addView(onetext_italic(context,"Ready to catch"));
    	        else if (td.current_stance==Stance_code.ST_RIPOSTE)
    	            ll.addView(onetext_italic(context,"Riposting"));
    	        else if (td.current_stance==Stance_code.ST_DEFLECT)
    	            ll.addView(onetext_italic(context,"Deflecting"));
    	        else if (td.current_stance==Stance_code.ST_RETRENCH)
    	            ll.addView(onetext_italic(context,"Retrenched"));
    	        else if (td.current_stance==Stance_code.ST_ALL_OUT_DEFENSE)
    	            ll.addView(onetext_italic(context,"Defending"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_PARALYSIS)
    	            ll.addView(onetext_italic(context,"Has used: Counter Paralysis"));
    	        else if (td.current_stance==Stance_code.ST_CHALICE)
    	            ll.addView(onetext_italic(context,"Has used: Chalice"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_DRAINING)
    	            ll.addView(onetext_italic(context,"Has used: Counter Draining"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_DARKNESS)
    	            ll.addView(onetext_italic(context,"Has used: Light"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_FEAR)
    	            ll.addView(onetext_italic(context,"Has used: Counter Fear"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_SLEEP)
    	            ll.addView(onetext_italic(context,"Has used: Counter Sleep"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_SILENCE)
    	            ll.addView(onetext_italic(context,"Has used: Counter Silence"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_BOLTS)
    	            ll.addView(onetext_italic(context,"Has used: Reflection"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_DEATHBLOW)
    	            ll.addView(onetext_italic(context,"Has used: Protect The Fallen"));
    	        else if (td.current_stance==Stance_code.ST_COUNTER_MANY)
    	            ll.addView(onetext_italic(context,"Has used: Distraction"));
    	        else if (td.current_stance==Stance_code.ST_TAKE_COVER)
    	            ll.addView(onetext_italic(context,"In cover"));
				if (td.has_status_effect(Status_effect_code.EF_ACCURATE))
    	            ll.addView(onetext_italic(context,"Accurate"));
				if (td.has_status_effect(Status_effect_code.EF_SPEED))
    	            ll.addView(onetext_italic(context,"Fast"));
				if (td.has_status_effect(Status_effect_code.EF_BATTLECRY))
    	            ll.addView(onetext_italic(context,"Raging"));
				if (td.has_status_effect(Status_effect_code.EF_REFLEXES))
    	            ll.addView(onetext_italic(context,"On guard"));
				if (td.has_status_effect(Status_effect_code.EF_RAPIDITY))
    	            ll.addView(onetext_italic(context,"Rapid fire"));
				if (td.has_status_effect(Status_effect_code.EF_RECKLESS))
    	            ll.addView(onetext_italic(context,"Reckless"));
				if (td.has_status_effect(Status_effect_code.EF_BLESSING))
    	            ll.addView(onetext_italic(context,"Blessed"));
				if (td.has_status_effect(Status_effect_code.EF_DODGE))
    	            ll.addView(onetext_italic(context,"Dodging"));
				if (td.has_status_effect(Status_effect_code.EF_LIFE_LEECH))
    	            ll.addView(onetext_italic(context,"Life leeching"));
    	        if (td.has_status_effect(Status_effect_code.EF_MEDITATION))
    	            ll.addView(onetext_italic(context,"Meditating"));
    	        if (td.has_status_effect(Status_effect_code.EF_EV))
    	            ll.addView(onetext_italic(context,"Warded"));
    	        if (td.has_status_effect(Status_effect_code.EF_FORCEBRAND))
    	            ll.addView(onetext_italic(context,"Force Brand"));
    	        if (td.has_status_effect(Status_effect_code.EF_SHIELD))
    	            ll.addView(onetext_italic(context,"Shielded"));
    	        if (td.has_status_effect(Status_effect_code.EF_REVEAL_DANGER))
    	            ll.addView(onetext_italic(context,"Find Traps"));
    	        if (td.has_status_effect(Status_effect_code.EF_FRONTROWBUFF))
    	            ll.addView(onetext_italic(context,"Exhorted"));
    	        if (td.has_status_effect(Status_effect_code.EF_RIGHTEOUSNESS))
    	            ll.addView(onetext_italic(context,"Righteous"));
    	        if (td.has_status_effect(Status_effect_code.EF_TAUNT))
    	            ll.addView(onetext_italic(context,"Taunting"));
    	        if (td.has_status_effect(Status_effect_code.EF_TAUNT3))
    	            ll.addView(onetext_italic(context,"Beacon"));
    	        if (td.has_status_effect(Status_effect_code.EF_INSULT))
    	            ll.addView(onetext_italic(context,"Insulting"));
    	        if (td.has_status_effect(Status_effect_code.EF_FIND_WEAKNESS))
    	            ll.addView(onetext_italic(context,"Find Weakness"));
    	        if (td.has_status_effect(Status_effect_code.EF_IMMUNE_FIRE))
    	            ll.addView(onetext_italic(context,"Fire immune"));
    	        else if (td.has_status_effect(Status_effect_code.EF_RESIST_FIRE) || td.fire_resistance)
    	            ll.addView(onetext_italic(context,"Fire resistant"));
    	        if (td.has_status_effect(Status_effect_code.EF_RESIST_COLD) || td.cold_resistance)
    	            ll.addView(onetext_italic(context,"Cold resistant"));
    	        if (td.lightning_resistance)
    	            ll.addView(onetext_italic(context,"Lightning resistant"));
    	        if (td.has_status_effect(Status_effect_code.EF_RESIST_POISON) || td.poison_resistance)
    	            ll.addView(onetext_italic(context,"Poison resistant"));
    	        if (td.sleep_resistance)
    	            ll.addView(onetext_italic(context,"Sleep resistant"));
    	        if (td.draining_resistance)
    	            ll.addView(onetext_italic(context,"Draining resistant"));
                for (Character_action a : td.actions){
                    if (a.action_code==Character_action_code.CA_FREE_ACTION){
        	            ll.addView(onetext_italic(context,"Paralysis resistant"));
                    }
                }
                for (Character_action a : td.actions){
                    if (a.action_code==Character_action_code.CA_STUN_RESISTANCE){
        	            ll.addView(onetext_italic(context,"Stun resistant"));
                    }
                }
            	if (Gamestate.p.fear_resistant()) ll.addView(onetext_italic(context,"Fear resistant"));
            	else{
                    for (Character_action a : td.actions){
                        if (a.action_code==Character_action_code.CA_RESIST_FEAR){
                        	ll.addView(onetext_italic(context,"Fear resistant"));
                        }
                    }
            	}
    	        if (td.has_status_effect(Status_effect_code.EF_PETRIFY))
    	            ll.addView(onetext_italic(context,"Petrified"));
    	        if (td.has_status_effect(Status_effect_code.EF_PARALYSIS))
    	            ll.addView(onetext_italic(context,"Paralysed"));
    	        if (td.has_status_effect(Status_effect_code.EF_POISON))
    	            ll.addView(onetext_italic(context,"Poisoned"));
    	        if (td.has_status_effect(Status_effect_code.EF_SLEEP))
    	            ll.addView(onetext_italic(context,"Asleep"));
    	        if (td.has_status_effect(Status_effect_code.EF_STUN))
    	            ll.addView(onetext_italic(context,"Stunned"));
    	        if (td.has_status_effect(Status_effect_code.EF_GRAPPLE))
    	            ll.addView(onetext_italic(context,"Grappled"));
    	        if (td.has_status_effect(Status_effect_code.EF_FEAR))
    	            ll.addView(onetext_italic(context,"Afraid"));
    	        if (td.has_status_effect(Status_effect_code.EF_CURSE))
    	            ll.addView(onetext_italic(context,"Cursed"));
    	        ll.addView(heading(context,"Stats"));
    	        if (td.class_code==Class_code.C_WARRIOR){
    	            ll.addView(twotext(context,"Strength:",(td.strength)));
    	            ll.addView(twotext(context,"Skill:",(td.skill)));
    	        } else if (td.class_code==Class_code.C_PRIEST){
    	            ll.addView(twotext(context,"Valor:",(td.valor)));
    	            ll.addView(twotext(context,"Wisdom:",(td.wisdom)));
    	        } else if (td.class_code==Class_code.C_ROGUE){
    	            ll.addView(twotext(context,"Daring:",(td.daring)));
    	            ll.addView(twotext(context,"Cunning:",(td.cunning)));
    	        } else if (td.class_code==Class_code.C_MAGE){
    	            ll.addView(twotext(context,"Power:",(td.power)));
    	            ll.addView(twotext(context,"Subtlety:",(td.subtlety)));
    	        }
    	        ll.addView(twotext(context,"Constitution:",(td.constitution)));
    	        ll.addView(twotext(context,"Energy:",(td.stamina)));
    	        ll.addView(twotext(context,"Reflexes:",(td.get_reflexes())));
    	        ll.addView(twotext(context,"Training:",(td.learning)));
    	        ll.addView(heading(context,"Defences"));
    	        ll.addView(twotext(context,"Armor:",(td.current_armor())));
    	        ll.addView(twotext(context,"Evasion:",(td.current_evasion())));
	            ll.addView(onetext(context,"DPS received in one-on-one fight:"));
	            ll.addView(onetext(context,"  Vs weak opponent:   "+ (round_to_2_dp(td.melee_dps_from(5,0,1,8))))); // melee period, accuracy, dice, sides
	            ll.addView(onetext(context,"  Vs skilful opponent:   "+ (round_to_2_dp(td.melee_dps_from(3,8,3,12))))); // melee period, accuracy, dice, sides	            		
	            ll.addView(onetext(context,"  Vs strong, clumsy opponent:   "+ (round_to_2_dp(td.melee_dps_from(4.5,2,10,20))))); // melee period, accuracy, dice, sides
    	        ll.addView(twotext(context,"Aggro:",(td.current_aggro())));
    	        if (td.melee_attack){
        	        ll.addView(heading(context,"Base melee attack"));
    	            if (td.fire_melee)
    	            	ll.addView(onetext_italic(context,"Fire attack (less effective against fire-resistant monsters)"));
    	            if (td.poison_melee)
    	            	ll.addView(onetext_italic(context,"Poison attack (less effective against poison-resistant monsters)"));
    	            if (td.dragon_slaying_melee)
    	            	ll.addView(onetext_italic(context,"+100% base damage vs dragons"));
    	            if (td.undead_slaying_melee)
    	            	ll.addView(onetext_italic(context,"+100% base damage vs undead"));
    	            ll.addView(twotext(context,"Accuracy:",td.current_accuracy()));
    	            ll.addView(twotext(context,"Rate of:","1 per " + round_to_1_dp(td.current_melee_period()/Constants.TIME_SCALAR) + " s"));
    	            ll.addView(twotext(context,"Damage:",(td.melee_dice) + "d" + (td.melee_sides) + " + " + (10*td.current_damage()) + "%"));
    	            ll.addView(onetext(context,"DPS vs armor 0 evasion 0:   "+ (round_to_2_dp(td.melee_dps_vs(0,0)))));
    	            ll.addView(onetext(context,"DPS vs armor 3 evasion 3:   "+ (round_to_2_dp(td.melee_dps_vs(3,3)))));
    	            ll.addView(onetext(context,"DPS vs armor 6 evasion 0:   "+ (round_to_2_dp(td.melee_dps_vs(6,0)))));
    	            ll.addView(onetext(context,"DPS vs armor 0 evasion 6:   "+ (round_to_2_dp(td.melee_dps_vs(0,6)))));
    	        }
    	        if (td.missile_attack){
        	        ll.addView(heading(context,"Base missile attack"));
    	            if (td.fire_missile)
    	            	ll.addView(onetext_italic(context,"Fire attack (less effective against fire-resistant monsters)"));
    	            if (td.poison_missile)
    	            	ll.addView(onetext_italic(context,"Poison attack (less effective against poison-resistant monsters)"));
    	            ll.addView(twotext(context,"Aim:",td.current_aim()));
    	            ll.addView(twotext(context,"Rate of:","1 per " + round_to_1_dp(td.current_missile_period()/Constants.TIME_SCALAR) + " s"));
    	            ll.addView(twotext(context,"Damage:",(td.missile_dice) + "d" + (td.missile_sides) + " + " + (10*td.current_penetration()) + "%"));
    	            ll.addView(onetext(context,"DPS vs armor 0 evasion 0:   "+ (round_to_2_dp(td.missile_dps_vs(0,0)))));
    	            ll.addView(onetext(context,"DPS vs armor 3 evasion 3:   "+ (round_to_2_dp(td.missile_dps_vs(3,3)))));
    	            ll.addView(onetext(context,"DPS vs armor 6 evasion 0:   "+ (round_to_2_dp(td.missile_dps_vs(6,0)))));
    	            ll.addView(onetext(context,"DPS vs armor 0 evasion 6:   "+ (round_to_2_dp(td.missile_dps_vs(0,6)))));
        		}
    	        if (td.actions.size()>0){
        	        ll.addView(heading(context,"Abilities"));
	                for (Character_action action : td.actions){
	                	if (!(action.get_description().equals(""))){
	                		ll.addView(twotext_firstitalic(context,action.name(),action.get_description()));
	                	}
	                }
    	        }
    	        sv.addView(ll);
    	        builder.setTitle(td.name)
    	            .setCancelable(true)
    	            .setOnCancelListener(new OnCancelListener() {public void onCancel(DialogInterface dialog) {activity.unpause_for_dialog();}})
    	            .setIcon(new BitmapDrawable(getResources(),LaunchActivity.gi.getImageBasic(td.image)))
    	            .setView(sv)
    	            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	            	public void onClick(DialogInterface dialog, int id) {
    	            		dialog.cancel();}});
       	        activity.pause_for_dialog();
       	        builder.create().show();
         	}
        	Gamestate.dragging = null;
        	Gamestate.mode=Game_mode.MODE_NORMAL;
        }

        public boolean onFling(MotionEvent original_event, MotionEvent event, float velocityX, float velocityY) {
        	if (Gamestate.dragging!=null){
            	for (Position_code pos : Arrays.asList(Position_code.POS_C_BACK_1,Position_code.POS_C_BACK_2,Position_code.POS_C_BACK_3,Position_code.POS_C_FRONT_1,Position_code.POS_C_FRONT_2,Position_code.POS_C_FRONT_3)){
            		if (pos_to_rect.get(pos).contains((int)event.getX(),(int)event.getY())){
            			Gamestate.p.move_character_to(Gamestate.dragging,pos);
            		}
            	}
        	}
        	Gamestate.dragging = null;
        	Gamestate.mode=Game_mode.MODE_NORMAL;
        	return true;
        }
    }

    public View onetext(Context context, String txt){
        TextView t = new TextView(context);
        t.setText(txt);
        return t;
    }

    public View onetext(Context context, int txt){
        return onetext(context,Integer.toString(txt));
    }

    public View onetext_italic(Context context, String txt){
        TextView t = new TextView(context);
        t.setText(txt);
        t.setTypeface(t.getTypeface(), Typeface.ITALIC);
        return t;
    }

    public View heading(Context context, String txt){
        TextView head1 = new TextView(context);
        head1.setText(txt);
        head1.setTypeface(head1.getTypeface(), Typeface.BOLD);
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        llp.setMargins(0, 16, 0, 0);
        head1.setLayoutParams(llp);
        return head1;
    }

    public View twotext_firstitalic(Context context,String txt1,String txt2){
        LinearLayout row1 = new LinearLayout(context);
        row1.setBackgroundColor(Color.argb(0,0,0,0));
        row1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        row1.setOrientation(LinearLayout.HORIZONTAL);
        row1.setGravity(Gravity.LEFT);
        TextView item1 = new TextView(context);
        item1.setText(txt1);
        item1.setWidth(145);
        item1.setTypeface(item1.getTypeface(), Typeface.ITALIC);
        row1.addView(item1);
        TextView item2 = new TextView(context);
        item2.setText(txt2);
        row1.addView(item2);
        return row1;
    }

    public View twotext(Context context,String txt1,String txt2){
        LinearLayout row1 = new LinearLayout(context);
        row1.setBackgroundColor(Color.argb(0,0,0,0));
        row1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
        row1.setOrientation(LinearLayout.HORIZONTAL);
        row1.setGravity(Gravity.LEFT);
        TextView item1 = new TextView(context);
        item1.setText(txt1);
        item1.setWidth(145);
        row1.addView(item1);
        TextView item2 = new TextView(context);
        item2.setText(txt2);
        row1.addView(item2);
        return row1;
    }

    public View twotext(Context context,String txt1,int txt2){
    	return twotext(context,txt1,Integer.toString(txt2));
    }

    static double round_to_1_dp(double x){
        return Math.round(x*10.0)/10.0;
    }

    static double round_to_2_dp(double x){
        return Math.round(x*100.0)/100.0;
    }

    public boolean onTouchEvent(MotionEvent event) {
	  return detector.onTouchEvent(event);
	}

	HashMap<Position_code,Rect> pos_to_rect, pos_to_health_bar_rect, pos_to_energy_bar_rect;
	Paint screenpaint;

	void init(){
	    pos_to_rect = new HashMap<Position_code,Rect>();
	    pos_to_rect.put(Position_code.POS_C_FRONT_1, new Rect(7, 16, 102, 111));
	    pos_to_rect.put(Position_code.POS_C_FRONT_2, new Rect(135, 1, 230, 96));
	    pos_to_rect.put(Position_code.POS_C_FRONT_3, new Rect(263, 16, 358, 111));
	    pos_to_rect.put(Position_code.POS_C_BACK_1, new Rect(7, 130, 102, 225));
	    pos_to_rect.put(Position_code.POS_C_BACK_2, new Rect(135, 115, 230, 210));
	    pos_to_rect.put(Position_code.POS_C_BACK_3, new Rect(263, 130, 358, 225));
	    pos_to_rect.put(Position_code.POS_DOWN, new Rect(135,430,230,525));
	    pos_to_health_bar_rect = new HashMap<Position_code,Rect>();
	    pos_to_energy_bar_rect = new HashMap<Position_code,Rect>();
	    pos_to_health_bar_rect.put(Position_code.POS_C_FRONT_1, new Rect(107, 16, 114, 111));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_FRONT_1, new Rect(120, 16, 127, 111));
	    pos_to_health_bar_rect.put(Position_code.POS_C_FRONT_2, new Rect(235, 1, 242, 96));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_FRONT_2, new Rect(248, 1, 255, 96));
	    pos_to_health_bar_rect.put(Position_code.POS_C_FRONT_3, new Rect(363, 16, 370, 111));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_FRONT_3, new Rect(376, 16, 383, 111));
	    pos_to_health_bar_rect.put(Position_code.POS_C_BACK_1, new Rect(107, 130, 114, 225));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_BACK_1, new Rect(120, 130, 127, 225));
	    pos_to_health_bar_rect.put(Position_code.POS_C_BACK_2, new Rect(235, 115, 242, 210));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_BACK_2, new Rect(248, 115, 255, 210));
	    pos_to_health_bar_rect.put(Position_code.POS_C_BACK_3, new Rect(363, 130, 370, 225));
	    pos_to_energy_bar_rect.put(Position_code.POS_C_BACK_3, new Rect(376, 130, 383, 225));
	    screenpaint = new Paint();
	    screenpaint.setStyle(Paint.Style.FILL);
    }

	void safeDrawBitmap(Canvas canvas,Bitmap b,Rect r){
		if (b!=null && r!=null){
			canvas.drawBitmap(b,null,r,null);
		}
	}

	protected void onDraw(Canvas canvas) {

	    if (!Gamestate.locked){
	    	Gamestate.locked = true;

			super.onDraw(canvas);
		    Paint paint = new Paint();
		    paint.setStyle(Paint.Style.FILL);
		    paint.setColor(Color.rgb(35, 35, 35));
		    canvas.drawPaint(paint);

	        boolean stealthed = false;
	        for (Character c : Gamestate.p.chars){
	            if (c.alive && !c.about_to_die && c.current_stance==Stance_code.ST_STEALTH)
	                stealthed = true;
	        }

		    for (Character c : Gamestate.p.chars){
		    	if (c.alive){
		    		Position_code pos = c.pos;
		    		Rect r = new Rect(0,0,0,0);
	                if (c.slide_from==null || c.slide_finish < Gamestate.w.clock){
	                    r = pos_to_rect.get(pos);
	                } else {
	                    double proportion = (Gamestate.w.clock-c.slide_start)/(c.slide_finish-c.slide_start);
	                    Rect r1 = pos_to_rect.get(c.slide_from);
	                    Rect r2 = pos_to_rect.get(c.slide_to);
	                    r.left = (int)Math.round((1-proportion) * r1.left + (proportion) * r2.left);
	                    r.right = (int)Math.round((1-proportion) * r1.right + (proportion) * r2.right);
	                    r.bottom = (int)Math.round((1-proportion) * r1.bottom + (proportion) * r2.bottom);
	                    r.top = (int)Math.round((1-proportion) * r1.top + (proportion) * r2.top);
	                }
	                if (!c.about_to_die){
	                    if (Gamestate.mode==Game_mode.MODE_FRIENDLY_TARGET && Gamestate.action.friend_target_ok(c)){
		                    safeDrawBitmap(canvas,LaunchActivity.gi.getImageBasic("green_frame"),r);
	                    }
	                }
		            Bitmap b = null;
	                if (c.has_status_effect(Status_effect_code.EF_PETRIFY))
	                    b = LaunchActivity.gi.getImageGrey(c.image);
	                else if (stealthed)
	                    b = LaunchActivity.gi.getImageDark(c.image);
	                else if (c.current_stance==Stance_code.ST_INVULNERABLE || c.has_status_effect(Status_effect_code.EF_INVULNERABLE))
	                    b = LaunchActivity.gi.getImageWhite(c.image);
	                else if (c.has_status_effect(Status_effect_code.EF_POISON) && c.status_effects.get(Status_effect_code.EF_POISON).level>0)
	                	b = LaunchActivity.gi.getImageGreen(c.image);
	                else
	                	b = LaunchActivity.gi.getImageBasic(c.image);
	                safeDrawBitmap(canvas,b,r);
	    			boolean colored = false;
	                if (c.flash_image!=null){
	                    if (c.flash_starts<0){
	                    	Bitmap f = LaunchActivity.gi.getImageBasic(c.flash_image);
	                    	safeDrawBitmap(canvas,f,r);
	                        colored = true;
	                        c.flash_starts = Gamestate.w.clock;
	                    } else if ((!c.flash_long && Gamestate.w.clock >= c.flash_starts + 0.2) || (Gamestate.w.clock >= c.flash_starts + 0.5)){
	                        c.flash_starts = -1;
	                        c.flash_image = null;
	                        c.flash_long = false;
	                    } else {
	                    	Bitmap f = LaunchActivity.gi.getImageBasic(c.flash_image);
	                    	safeDrawBitmap(canvas,f,r);
	                        colored = true;
	                    }
	                }
	                if (c.drowning){
	                	double drowning_prop = Math.min(1.0,(Gamestate.w.clock - c.drowning_from)/60.0);
		    			Rect water_rect = new Rect(r.left,r.bottom-(int)(drowning_prop*96),r.right,r.bottom);
		    			paint.setColor(Color.argb(100,0,50,150));
		    			canvas.drawRect(water_rect, paint);
	                }
	                if (c.webbed_until>Gamestate.w.clock){
                    	Bitmap f = LaunchActivity.gi.getImageBasic("spiderweb_a_96_01");
                    	safeDrawBitmap(canvas,f,r);	                	
	                }
	                if (c.flash_number>0 && c.number_starts>0 && !(c.flash_starts>0 && c.flash_image!=null)){
	                    if (Gamestate.w.clock >= c.number_starts + 1.7){
	                        c.flash_number = -1;
	                        c.number_starts = -1;
	                    } else {
	                    	paint.setColor(c.number_color);
	                	    paint.setAlpha((int)Math.min(240,340 - (int)Math.floor(200 * (Gamestate.w.clock - c.number_starts))));
	                	    paint.setTextSize(36);
	                	    paint.setAntiAlias(true);
	                	    paint.setTextAlign(Align.CENTER);
	                	    float width = paint.measureText(Integer.toString(c.flash_number));
	                	    canvas.drawText(Integer.toString(c.flash_number), r.left+48, r.top+47, paint);
	                    }
	                }
	                if (c.about_to_die && !(c.flash_starts>0 && c.flash_image!=null)){
	                	Bitmap sk = LaunchActivity.gi.getImageBasic("skull_crossbones");
	                	Rect r2 = new Rect(r.left+28,r.top+52,r.left+27+sk.getWidth(),r.top+51+sk.getHeight());
	                	safeDrawBitmap(canvas,sk,r2);
	                } else {
	                	if (Gamestate.mode == Game_mode.MODE_NORMAL){
		                    if (!colored && !c.has_status_effect(Status_effect_code.EF_PETRIFY) && (!c.can_act() || c.has_status_effect(Status_effect_code.EF_SLEEP) || c.has_status_effect(Status_effect_code.EF_GRAPPLE))){
		                        Bitmap g = LaunchActivity.gi.getImageBasic("grey");
		                        safeDrawBitmap(canvas,g,r);
		                    }
	                	}
	                }
	                ArrayList<String> effects_to_show = new ArrayList<String>();
	                int n_effects_shown = 0;
	                // no more than 11 characters please
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_IMMUNE_FIRE)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("FIRE IMMUNE");
		            }
		            else if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RESIST_FIRE)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("FIRE RESIST");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RESIST_COLD)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("COLD RESIST");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RESIST_POISON)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("POISON RESIST");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_PARALYSIS) && c.webbed_until==0){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("PARALYSED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_SLEEP)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("ASLEEP");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_STUN)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("STUNNED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_CURSE)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("CURSED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_FEAR)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("AFRAID");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_GRAPPLE)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("GRAPPLED");
		            }
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_ACCURATE)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("ACCURATE");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_SPEED)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("FAST");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_BATTLECRY)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("RAGING");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_REFLEXES)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("ON GUARD");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RAPIDITY)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("RAPID FIRE");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RECKLESS)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("RECKLESS");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_LIFE_LEECH)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("LIFE LEECH");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_BLESSING)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("BLESSED");
					}
					if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_DODGE)){
					    n_effects_shown = n_effects_shown + 1;
					    effects_to_show.add("DODGING");
					}
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_MEDITATION)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("MEDITATING");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_EV)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("WARDED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_SHIELD)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("SHIELDED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_FORCEBRAND)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("FORCE BRAND");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_FRONTROWBUFF)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("BUFFED");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_RIGHTEOUSNESS)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("RIGHTEOUS");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_TAUNT)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("TAUNTING");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_TAUNT3)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("BEACON");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_INSULT)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("INSULTING");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_FIND_WEAKNESS)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("PASS ARMOR");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_BERSERK){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("BERSERK");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_RETRENCH){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("RESTING");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_ALL_OUT_DEFENSE){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("DEFENDING");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_CATCH){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("CATCHING");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_TAKE_COVER){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("IN COVER");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_DEFLECT){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("DEFLECT");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_BLOCK){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("BLOCK");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_EPIC_BLOCK){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("EPIC BLOCK");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_LEAP_ASIDE){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("LEAP ASIDE");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_RIPOSTE){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("RIPOSTE");
		            }
		            if (n_effects_shown<4 && (c.current_stance == Stance_code.ST_COUNTER_PARALYSIS || c.current_stance == Stance_code.ST_COUNTER_DARKNESS || c.current_stance == Stance_code.ST_COUNTER_DRAINING || c.current_stance == Stance_code.ST_COUNTER_FEAR || c.current_stance == Stance_code.ST_COUNTER_SLEEP || c.current_stance == Stance_code.ST_COUNTER_SILENCE || c.current_stance == Stance_code.ST_COUNTER_BOLTS || c.current_stance == Stance_code.ST_COUNTER_DEATHBLOW || c.current_stance == Stance_code.ST_COUNTER_MANY)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("COUNTER");
		            }
		            if (n_effects_shown<4 && c.current_stance == Stance_code.ST_CHALICE){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("CHALICE");
		            }
		            if (n_effects_shown<4 && c.has_status_effect(Status_effect_code.EF_REVEAL_DANGER)){
		                n_effects_shown = n_effects_shown + 1;
		                effects_to_show.add("SEARCHING");
		            }
	            	paint.setColor(Color.rgb(250,220,100));
	        	    paint.setAlpha(255);
	        	    paint.setTextSize(12);
	        	    paint.setAntiAlias(true);
	        	    paint.setTextAlign(Align.LEFT);
	                if (n_effects_shown>0){
	            	    canvas.drawText(effects_to_show.get(0), r.left+3, r.top+92, paint);}
	                if (n_effects_shown>1){
	            	    canvas.drawText(effects_to_show.get(1), r.left+3, r.top+77, paint);}
	                if (n_effects_shown>2){
	            	    canvas.drawText(effects_to_show.get(2), r.left+3, r.top+62, paint);}
	                if (n_effects_shown>3){
	            	    canvas.drawText(effects_to_show.get(3), r.left+3, r.top+47, paint);}
	    			if ((c.slide_from==null || c.slide_finish < Gamestate.w.clock) && !c.has_status_effect(Status_effect_code.EF_PETRIFY)){
	    				// do bars
		    			Rect entire_health_rect = pos_to_health_bar_rect.get(pos);
		    			Rect entire_energy_rect = pos_to_energy_bar_rect.get(pos);
		    			float health_frac = (float)(c.health) / c.max_health;
		    			float energy_frac = (float)(c.energy) / c.max_energy;
		    			int health_color;
		    			int energy_color;
		    			if (health_frac>0.7) health_color = Color.rgb(20,200,20);
		    			else if (health_frac<0.3) health_color = Color.rgb(200,20,20);
		    			else health_color = Color.rgb((int)Math.round(20+180*(1-((health_frac-0.3)/0.4))),(int)Math.round(200-180*(1-((health_frac-0.3)/0.4))),20);
		    			if (energy_frac>0.7) energy_color = Color.rgb(150,200,250);
		    			else if (energy_frac<0.3) energy_color = Color.rgb(0,20,100);
		    			else energy_color = Color.rgb((int)Math.round(150-150*(1-((energy_frac-0.3)/0.4))),(int)Math.round(200-180*(1-((energy_frac-0.3)/0.4))),(int)Math.round(250-150*(1-((energy_frac-0.3)/0.4))));
		    			int health_height = (int)Math.ceil(c.health*(double)(entire_health_rect.height())/c.max_health);
		    			int energy_height = (int)Math.ceil(c.energy*(double)(entire_energy_rect.height())/c.max_energy);
		    			Rect used_health_rect = new Rect(entire_health_rect.left,entire_health_rect.bottom-health_height+1,entire_health_rect.right,entire_health_rect.bottom);
		    			Rect used_energy_rect = new Rect(entire_energy_rect.left,entire_energy_rect.bottom-energy_height+1,entire_energy_rect.right,entire_energy_rect.bottom);
		    			paint.setColor(health_color);
		    			if (health_height>0)
		    				canvas.drawRect(used_health_rect, paint);
		    			paint.setColor(energy_color);
		    			if (energy_height>0)
		    				canvas.drawRect(used_energy_rect, paint);
	    			}
		          /*  if (Gamestate.dragging!=null && Gamestate.dragging.number!=c.number && Gamestate.w.clock>=(Gamestate.started_dragging+0.2)){
		            	Gamestate.mode=Game_mode.MODE_DRAGGING;
	                    if (!(Gamestate.dragging.front_row() && Gamestate.p.n_surviving_front_chars()==1 && (Gamestate.p.get_char_at_pos(pos)==null || !Gamestate.p.get_char_at_pos(pos).can_act()) && (pos==Position_code.POS_C_BACK_1 || pos==Position_code.POS_C_BACK_2 || pos==Position_code.POS_C_BACK_3))){
		                	if (!(Gamestate.p.get_char_at_pos(pos)!=null && !(Gamestate.p.get_char_at_pos(pos).can_act()))){
		                    	Bitmap f = GameImages.getImageBasic("blue_frame");
		                    	safeDrawBitmap(canvas,f,r);
		                    }
		                }
		            } */
		    	}
		    }
		    if (Gamestate.w.boulder_dropped>0 && Gamestate.w.boulder_flash){
		    	Rect r = pos_to_rect.get(Gamestate.w.boulder_pos);
                Bitmap b = LaunchActivity.gi.getImageBasic("red_frame");
                safeDrawBitmap(canvas,b,r);
		    }
	        for (int i=0; i<Gamestate.p.chars.size(); i++){
	        	Character c = Gamestate.p.chars.get(i);
	            if (!c.alive && c.slide_from!=null && c.slide_finish >= Gamestate.w.clock){
	                Rect r = new Rect(0,0,0,0);
	                double proportion = (Gamestate.w.clock-c.slide_start)/(c.slide_finish-c.slide_start);
	                Rect r1 = pos_to_rect.get(c.slide_from);
	                Rect r2 = pos_to_rect.get(c.slide_to);
	                r.left = (int)Math.round((1-proportion) * r1.left + (proportion) * r2.left);
	                r.right = (int)Math.round((1-proportion) * r1.right + (proportion) * r2.right);
	                r.bottom = (int)Math.round((1-proportion) * r1.bottom + (proportion) * r2.bottom);
	                r.top = (int)Math.round((1-proportion) * r1.top + (proportion) * r2.top);
	                Bitmap b = LaunchActivity.gi.getImageBasic(c.image);
	                safeDrawBitmap(canvas,b,r);
	            }
	        }
	        if (Gamestate.w.show_flash_image_in_party_until > Gamestate.w.clock){
	        	Rect r = pos_to_rect.get(Gamestate.w.show_flash_image_in_party_pos);
	        	Bitmap b = LaunchActivity.gi.getImageBasic(Gamestate.w.show_flash_image_in_party_image);
                safeDrawBitmap(canvas,b,r);
	        }
    		for (int i=0; i<Gamestate.w.darkness; i++){
	    	    screenpaint.setColor(Color.BLACK);
	    	    screenpaint.setAlpha(85);
	    	    canvas.drawPaint(screenpaint);
	        }
	    	if (Gamestate.w.slow_time_until >= Gamestate.w.clock){
	    		screenpaint.setColor(Color.BLUE);
	    		screenpaint.setAlpha(50);
	    	    canvas.drawPaint(screenpaint);
	    	}
	    	if (Gamestate.w.dark_flare>0){
	    		if (Gamestate.w.dark_flare>Gamestate.w.clock){
		    		screenpaint.setColor(Color.BLACK);
		    		screenpaint.setAlpha(200);
		    	    canvas.drawPaint(screenpaint);	    			
	    		} else {
	    			Gamestate.w.dark_flare = 0;
	    		}
	    	}
	    	if (Gamestate.w.bright_flare>0){
	    		if (Gamestate.w.bright_flare>Gamestate.w.clock){
		    		screenpaint.setColor(Color.WHITE);
		    		screenpaint.setAlpha(200);
		    	    canvas.drawPaint(screenpaint);	    			
	    		} else {
	    			Gamestate.w.bright_flare = 0;
	    		}
	    	}
	    	Gamestate.locked = false;
	    }
	}
}
